//
//  SWGeneralMusicCell.swift
//  ChildStory
//
//  Created by linhan on 2016/12/6.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWGeneralMusicCell: UITableViewCell
{
    weak var music:SWMusic?
    
    private var _change:Bool = true
    private var _cellWidth:CGFloat = 0
    
    private var _titleLabel:UILabel = UILabel()
    private var _durationLabel:UILabel = UILabel()
    private var _artistLabel:UILabel = UILabel()
    private var _imageView:ImageLoader = ImageLoader()
    
    init(style: UITableViewCellStyle, reuseIdentifier: String?, cellWidth:CGFloat)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        _cellWidth = cellWidth
        setup()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        let leftMargin:CGFloat = 15
        let rightMargin:CGFloat = 15
        
        
        _imageView.frame = CGRectMake(leftMargin, 11, 38, 38)
        _imageView.options.contentMode = .scaleAspectFill
        contentView.addSubview(_imageView)
        
        let durationLabelWidth:CGFloat = 50
        let durationLabelX:CGFloat = _cellWidth - rightMargin - durationLabelWidth
        _durationLabel.font = UIFont.systemFont(ofSize: 12)
        _durationLabel.textAlignment = .right
        _durationLabel.textColor = UIColor(hex: 0x2f3231)
        _durationLabel.frame = CGRectMake(durationLabelX, 9, durationLabelWidth, 16)
        contentView.addSubview(_durationLabel)
        
        let titleLabelX:CGFloat = _imageView.right + 23
        _titleLabel.font = UIFont.systemFont(ofSize: 17)
        _titleLabel.textColor = UIColor(hex: 0x2f3231)
        _titleLabel.frame = CGRectMake(titleLabelX, 9, durationLabelX - titleLabelX, 21)
        contentView.addSubview(_titleLabel)
        
        _artistLabel.font = UIFont.systemFont(ofSize: 12)
        _artistLabel.textColor = UIColor(hex: 0xd8d8d8)
        _artistLabel.frame = CGRectMake(titleLabelX, _titleLabel.bottom + 7, _titleLabel.width, 16)
        contentView.addSubview(_artistLabel)
    }
    
    override func prepareForReuse()
    {
        super.prepareForReuse()
        _change = true
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        if let music = music , _change
        {
            _change = false
            _titleLabel.text = music.name
            _durationLabel.text = music.durationString
            _artistLabel.text = music.artist
            _imageView.load(music.thumbURL ?? "")
        }
    }
    
}
